#include <STC15F2K60S2.H>

sbit key_han_1 = P3^0; 
sbit key_han_2 = P3^1; 
sbit key_han_3 = P3^2; 
sbit key_han_4 = P3^3; 

sbit key_lie_1 = P4^4;
sbit key_lie_2 = P4^2;
sbit key_lie_3 = P3^5;
sbit key_lie_4 = P3^4;

static unsigned char KeyNumber = 0;
static unsigned char key_n[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
	
unsigned char key_return(void)
{
	unsigned char k = 0;
	if(KeyNumber)
	k = KeyNumber;
	KeyNumber = 0;
	return k;
}

void key_loop(void)
{
	static unsigned char k_count_h = 0;
	//static unsigned char key_now[4][4] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},key_last[4][4] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	key_lie_1 = key_lie_2 = key_lie_3 = key_lie_4 = 1;
	switch(k_count_h)
	{
		case 0:key_han_1 = 0;key_han_2 = 1;key_han_3 = 1;key_han_4 = 1;
					 if(key_lie_1 == 0)KeyNumber = key_n[k_count_h][0];
					 if(key_lie_2 == 0)KeyNumber = key_n[k_count_h][1];
					 if(key_lie_3 == 0)KeyNumber = key_n[k_count_h][2];
					 if(key_lie_4 == 0)KeyNumber = key_n[k_count_h][3];
		break;
		case 1:key_han_1 = 1,key_han_2 = 0,key_han_3 = 1,key_han_4 = 1;
					 if(key_lie_1 == 0)KeyNumber = key_n[k_count_h][0];
					 if(key_lie_2 == 0)KeyNumber = key_n[k_count_h][1];
					 if(key_lie_3 == 0)KeyNumber = key_n[k_count_h][2];
					 if(key_lie_4 == 0)KeyNumber = key_n[k_count_h][3];
		break;
		case 2:key_han_1 = 1,key_han_2 = 1,key_han_3 = 0,key_han_4 = 1;
					 if(key_lie_1 == 0)KeyNumber = key_n[k_count_h][0];
					 if(key_lie_2 == 0)KeyNumber = key_n[k_count_h][1];
					 if(key_lie_3 == 0)KeyNumber = key_n[k_count_h][2];
					 if(key_lie_4 == 0)KeyNumber = key_n[k_count_h][3];
		break;
		case 3:key_han_1 = 1,key_han_2 = 1,key_han_3 = 1,key_han_4 = 0;
					 if(key_lie_1 == 0)KeyNumber = key_n[k_count_h][0];
					 if(key_lie_2 == 0)KeyNumber = key_n[k_count_h][1];
					 if(key_lie_3 == 0)KeyNumber = key_n[k_count_h][2];
					 if(key_lie_4 == 0)KeyNumber = key_n[k_count_h][3];
		break;
	}

	k_count_h++;

	if(k_count_h == 4)
		k_count_h = 0;
}